package com.axent.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.ToString;

/**
 * 封装返回前端格式
 *
 * @author ww
 * @date 2018/8/1
 */
@Getter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultUtil <T>{

    /**返回的状态码*/
    private int code;
    /**返回的状态*/
    private String msg;
    /** 数据总数 */
    private Integer count;
    /**返回的数据*/
    private T data;

    public ResultUtil() {
    }

    private ResultUtil(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    private ResultUtil(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ResultUtil(int code, String msg, Integer count, T data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    /**
     * 无返回数据，只有状态码和错误类型
     * Created by ww on 2018/8/2 10:47
     * @return 返回的数据
     */
    public static ResultUtil error(ResultEnum resultEum){
        return new ResultUtil<>(resultEum.getCode(), resultEum.getMsg());
    }

    /**
     * 无返回数据，只有状态
     * Created by ww on 2018/8/2 10:47
     * @return 返回的数据
     */
    public static ResultUtil success(){
        return new ResultUtil<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
    }

    /**
     * 没有总数返回，只有数据
     * Created by ww on 2018/8/2 10:47
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultUtil success(W data){
        return new ResultUtil<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 自定义返回状态码，只有数据
     * Created by ww on 2018/8/2 10:47
     * @param resultEnum 自定义返回状态码
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultUtil success(ResultEnum resultEnum, W data){
        return new ResultUtil<>(resultEnum.getCode(), resultEnum.getMsg(), data);
    }

    /**
     * 正常返回数据格式,含分页总数
     * Created by ww on 2018/8/2 10:48
     * @param count 返回结果描述
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultUtil success(Integer count, W data){
        return new ResultUtil<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), count, data);
    }

    /**
     * 自定义返回状态码类型,含分页总数
     * Created by ww on 2018/8/2 10:48
     * @param resultEnum 自定义返回状态码
     * @param count 返回结果描述
     * @param data 数据
     * @return 返回的数据
     */
    public static <W> ResultUtil success(ResultEnum resultEnum, Integer count, W data){
        return new ResultUtil<>(resultEnum.getCode(), resultEnum.getMsg(), count, data);
    }
}
